package aor2.cpu.blockcontrol;

import aor2.components.Restorable;
import aor2.components.SignalSet;
import aor2.cpu.blockregisters.InstructionSignal;

/**
 * Skup signala uslova
 * 
 * @author Moxi
 * 
 */
public class ConditionSignalSet extends SignalSet implements Restorable {
	private static ConditionSignalSet singleton = null;

	private ConditionSignalSet() {
		super();

		add("adr0");
		add("adr1");
		add("notpom1");
		add("notpom2");

		add("regdir1");
		add("postinc1");
		add("predec1");
		add("index1");
		add("regind1");
		add("predecind1");
		add("postincind1");
		add("indexind1");

		add("regdir2");
		add("postinc2");
		add("predec2");
		add("index2");
		add("regind2");
		add("predecind2");
		add("postincind2");
		add("indexind2");

		add("immed2");
		add("eql");

		add("PRINS");
		add("PRCOD", InstructionSignal.UNKNOWN.getSignal());
		add("PRADR");
		add("PRINM");
		add("PRIMM");

		add("MOV", InstructionSignal.MOV.getSignal());
		add("SUB", InstructionSignal.SUB.getSignal());
		add("OR", InstructionSignal.OR.getSignal());

		add("INC", InstructionSignal.INC.getSignal());
		add("DEC", InstructionSignal.DEC.getSignal());
		add("NOT", InstructionSignal.NOT.getSignal());
		add("ASR", InstructionSignal.ASR.getSignal());
		add("JMPIND", InstructionSignal.JMPIND.getSignal());
		add("JSRIND", InstructionSignal.JSRIND.getSignal());

		add("BRN", InstructionSignal.BRN.getSignal());
		add("BSR", InstructionSignal.BSR.getSignal());
		add("BNZ", InstructionSignal.BNZ.getSignal());
		add("INT", InstructionSignal.INT.getSignal());

		add("RTI", InstructionSignal.RTI.getSignal());
		add("RTS", InstructionSignal.RTS.getSignal());
		add("INTE", InstructionSignal.INTE.getSignal());
		add("INTD", InstructionSignal.INTD.getSignal());
		add("TRPE", InstructionSignal.TRPE.getSignal());
		add("TRPD", InstructionSignal.TRPD.getSignal());

		add("run", true);

		add("notPrekid", true);
	}

	public static ConditionSignalSet getInstance() {
		if (singleton == null)
			singleton = new ConditionSignalSet();
		return singleton;
	}

	@Override
	public void restore(String restorePoint) {
		String trim = restorePoint.replace(beginTag(), "");
		trim = trim.replace(endTag(), "");
		trim = trim.trim();
		String[] states = trim.split(" ");
		int i = 0;

		get("adr0").clearHistory();
		get("adr1").clearHistory();
		get("notpom1").clearHistory();
		get("notpom2").clearHistory();

		get("regdir1").clearHistory();
		get("postinc1").clearHistory();
		get("predec1").clearHistory();
		get("index1").clearHistory();
		get("regind1").clearHistory();
		get("predecind1").clearHistory();
		get("postincind1").clearHistory();
		get("indexind1").clearHistory();

		get("regdir2").clearHistory();
		get("postinc2").clearHistory();
		get("predec2").clearHistory();
		get("index2").clearHistory();
		get("regind2").clearHistory();
		get("predecind2").clearHistory();
		get("postincind2").clearHistory();
		get("indexind2").clearHistory();

		get("immed2").clearHistory();
		get("eql").clearHistory();

		get("PRINS").clearHistory();
		get("PRCOD").clearHistory();
		get("PRADR").clearHistory();
		get("PRINM").clearHistory();
		get("PRIMM").clearHistory();

		get("MOV").clearHistory();
		get("SUB").clearHistory();
		get("OR").clearHistory();

		get("INC").clearHistory();
		get("DEC").clearHistory();
		get("NOT").clearHistory();
		get("ASR").clearHistory();
		get("JMPIND").clearHistory();
		get("JSRIND").clearHistory();

		get("BRN").clearHistory();
		get("BSR").clearHistory();
		get("BNZ").clearHistory();
		get("INT").clearHistory();

		get("RTI").clearHistory();
		get("RTS").clearHistory();
		get("INTE").clearHistory();
		get("INTD").clearHistory();
		get("TRPE").clearHistory();
		get("TRPD").clearHistory();

		get("run").clearHistory();

		get("notPrekid").clearHistory();

		get("adr0").setActive(Boolean.parseBoolean(states[i++]));
		get("adr1").setActive(Boolean.parseBoolean(states[i++]));
		get("notpom1").setActive(Boolean.parseBoolean(states[i++]));
		get("notpom2").setActive(Boolean.parseBoolean(states[i++]));

		get("regdir1").setActive(Boolean.parseBoolean(states[i++]));
		get("postinc1").setActive(Boolean.parseBoolean(states[i++]));
		get("predec1").setActive(Boolean.parseBoolean(states[i++]));
		get("index1").setActive(Boolean.parseBoolean(states[i++]));
		get("regind1").setActive(Boolean.parseBoolean(states[i++]));
		get("predecind1").setActive(Boolean.parseBoolean(states[i++]));
		get("postincind1").setActive(Boolean.parseBoolean(states[i++]));
		get("indexind1").setActive(Boolean.parseBoolean(states[i++]));

		get("regdir2").setActive(Boolean.parseBoolean(states[i++]));
		get("postinc2").setActive(Boolean.parseBoolean(states[i++]));
		get("predec2").setActive(Boolean.parseBoolean(states[i++]));
		get("index2").setActive(Boolean.parseBoolean(states[i++]));
		get("regind2").setActive(Boolean.parseBoolean(states[i++]));
		get("predecind2").setActive(Boolean.parseBoolean(states[i++]));
		get("postincind2").setActive(Boolean.parseBoolean(states[i++]));
		get("indexind2").setActive(Boolean.parseBoolean(states[i++]));

		get("immed2").setActive(Boolean.parseBoolean(states[i++]));
		get("eql").setActive(Boolean.parseBoolean(states[i++]));

		get("PRINS").setActive(Boolean.parseBoolean(states[i++]));
		get("PRCOD").setActive(Boolean.parseBoolean(states[i++]));
		get("PRADR").setActive(Boolean.parseBoolean(states[i++]));
		get("PRINM").setActive(Boolean.parseBoolean(states[i++]));
		get("PRIMM").setActive(Boolean.parseBoolean(states[i++]));

		get("MOV").setActive(Boolean.parseBoolean(states[i++]));
		get("SUB").setActive(Boolean.parseBoolean(states[i++]));
		get("OR").setActive(Boolean.parseBoolean(states[i++]));

		get("INC").setActive(Boolean.parseBoolean(states[i++]));
		get("DEC").setActive(Boolean.parseBoolean(states[i++]));
		get("NOT").setActive(Boolean.parseBoolean(states[i++]));
		get("ASR").setActive(Boolean.parseBoolean(states[i++]));
		get("JMPIND").setActive(Boolean.parseBoolean(states[i++]));
		get("JSRIND").setActive(Boolean.parseBoolean(states[i++]));

		get("BRN").setActive(Boolean.parseBoolean(states[i++]));
		get("BSR").setActive(Boolean.parseBoolean(states[i++]));
		get("BNZ").setActive(Boolean.parseBoolean(states[i++]));
		get("INT").setActive(Boolean.parseBoolean(states[i++]));

		get("RTI").setActive(Boolean.parseBoolean(states[i++]));
		get("RTS").setActive(Boolean.parseBoolean(states[i++]));
		get("INTE").setActive(Boolean.parseBoolean(states[i++]));
		get("INTD").setActive(Boolean.parseBoolean(states[i++]));
		get("TRPE").setActive(Boolean.parseBoolean(states[i++]));
		get("TRPD").setActive(Boolean.parseBoolean(states[i++]));

		get("run").setActive(Boolean.parseBoolean(states[i++]));

		get("notPrekid").setActive(Boolean.parseBoolean(states[i++]));

	}

	@Override
	public String createRestorePoint() {
		return beginTag() + "\n\t" + get("adr0").isActive() + " "
				+ get("adr1").isActive() + " " + get("notpom1").isActive()
				+ " " + get("notpom2").isActive() + " " +

				get("regdir1").isActive() + " " + get("postinc1").isActive()
				+ " " + get("predec1").isActive() + " "
				+ get("index1").isActive() + " " + get("regind1").isActive()
				+ " " + get("predecind1").isActive() + " "
				+ get("postincind1").isActive() + " "
				+ get("indexind1").isActive() + " " +

				get("regdir2").isActive() + " " + get("postinc2").isActive()
				+ " " + get("predec2").isActive() + " "
				+ get("index2").isActive() + " " + get("regind2").isActive()
				+ " " + get("predecind2").isActive() + " "
				+ get("postincind2").isActive() + " "
				+ get("indexind2").isActive() + " " +

				get("immed2").isActive() + " " + get("eql").isActive() + " " +

				get("PRINS").isActive() + " " + get("PRCOD").isActive() + " "
				+ get("PRADR").isActive() + " " + get("PRINM").isActive() + " "
				+ get("PRIMM").isActive() + " " +

				get("MOV").isActive() + " " + get("SUB").isActive() + " "
				+ get("OR").isActive() + " " +

				get("INC").isActive() + " " + get("DEC").isActive() + " "
				+ get("NOT").isActive() + " " + get("ASR").isActive() + " "
				+ get("JMPIND").isActive() + " " + get("JSRIND").isActive()
				+ " " +

				get("BRN").isActive() + " " + get("BSR").isActive() + " "
				+ get("BNZ").isActive() + " " + get("INT").isActive() + " " +

				get("RTI").isActive() + " " + get("RTS").isActive() + " "
				+ get("INTE").isActive() + " " + get("INTD").isActive() + " "
				+ get("TRPE").isActive() + " " + get("TRPD").isActive() + " " +

				get("run").isActive() + " " +

				get("notPrekid").isActive() + "\n" + endTag();
	}

	@Override
	public String beginTag() {
		return "<ConditionSignalSet>";
	}

	@Override
	public String endTag() {
		return "</ConditionSignalSet>";
	}

	public static void main(String[] args) {
		ConditionSignalSet css = ConditionSignalSet.getInstance();

		String res = css.createRestorePoint();
		System.out.println(res);

		css.restore(res);

		res = css.createRestorePoint();
		System.out.println(res);

	}
}
